﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加会员-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../static/css/font.css">
    <link rel="stylesheet" href="../../../static/css/weadmin.css">
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body>
    <div class="layui-form-item">
        <div class="layui-input-block layui-upload">
            <button type="button" class="layui-btn" id="btn_UploadImage">上传文件</button>
        </div>
    </div>
    <div id="VueUploadFiles" class="Upload-ul">
        <ul>
            <li class="file-file" v-for="file in files">
                <a v-bind:href="file.url">{{file.name}}</a>
                <i v-if="!file.isUpload" class="layui-icon layui-anim layui-anim-rotate layui-anim-loop" style="color: green;">&#xe63d;</i>
                <button class="layui-btn layui-btn-xs Upload-layui-btn" v-bind:id="file.name" onclick="deleteFileClick(this.id)">
                    <i class="layui-icon">&#x1006;</i>
                </button>
            </li>
        </ul>
        <div class="file-image" v-for="file in images">
            <img v-bind:src="file.url" />
            <i v-if="!file.isUpload" class="layui-icon layui-anim layui-anim-rotate layui-anim-loop" style="color: green;">&#xe63d;</i>
            <button class="layui-btn layui-btn-xs Upload-layui-btn" v-bind:id="file.name" onclick="deleteFileClick(this.id)">
                <i class="layui-icon">&#x1006;</i>
            </button>
        </div>
        <div style="height: 1px; width: 100%; clear: both"></div>
    </div>

    <script src="../../../lib/layui/layui.js" charset="utf-8"></script>
    <style>

        .Upload-ul {
            background-color: rgba(172, 172, 172, 0.137);
            padding: 10px 10px;
            clear: both;
        }

            .Upload-ul .file-file {
                position: relative;
                margin-top: 5px;
                margin-bottom: 5px;
                margin-left: 0.5%;
                margin-right: 0.5%;
                padding: 10px;
                padding-right: 50px;
                background-color: rgba(172, 172, 172, 0.1);
                border: 1px rgba(172, 172, 172, 0.1) solid;
                border-radius: 4px;
                -webkit-user-select: none;
                -moz-user-select: none;
                -ms-user-select: none;
                user-select: none;
            }

                .Upload-ul .file-file a {
                    height: 20px;
                    line-height: 20px;
                }

                .Upload-ul .file-file button {
                    position: absolute;
                    right: 10px;
                    top: 50%;
                    transform: translate(0%, -50%);
                    z-index: 100;
                    padding: 1px 2px 0px 4px;
                    background-color: rgba(172, 172, 172, 0.0);
                    color: gray;
                }

        .Upload-layui-btn:hover {
            color: red;
        }

        .Upload-ul .file-image {
            position: relative;
            margin-top: 5px;
            margin-bottom: 5px;
            padding: 5px;
            background-color: rgba(172, 172, 172, 0.1);
            border: 1px rgba(172, 172, 172, 0.1) solid;
            border-radius: 4px;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
            float: left;
            margin-left: 0.5%;
            margin-right: 0.5%;
            width: 32.333%;
            box-sizing: border-box;
        }

            .Upload-ul .file-image img {
                width: 80px;
                height: 80px;
            }

            .Upload-ul .file-image button {
                position: absolute;
                right: 10px;
                top: 50%;
                transform: translate(0%, -50%);
                z-index: 100;
                padding: 1px 2px 0px 4px;
                background-color: rgba(172, 172, 172, 0.0);
                color: gray;
            }
    </style>
     
    <script>
        var vueUploadFiles;
        $(function () {
            vueUploadFiles = new Vue({
                el: '#VueUploadFiles',
                data: {
                    files: [],
                    images: [],
                }
            });
            layui.use('upload', function () {
                var upload = layui.upload;
                //执行实例
                var uploadInst = upload.render({
                    elem: '#btn_UploadImage',//绑定元素
                    url: '../Controller/UploadFiles.ashx',//上传接口
                    multiple: 'true',
                    accept: "images|file|video|audio",
                    exts: 'jpg|png|gif|bmp|jpeg|zip|rar|7z|pdf|doc|docx|xls|xlsx|avi|mp4|rmvb|wmv|txt|pub|pptx|ppt',
                    data: {
                        cmd: "UploadSomeFiles"
                    },
                    choose: function (obj) {
                        //将每次选择的文件追加到文件队列
                        var files = obj.pushFile();
                        //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                        obj.preview(function (index, file, result) {
                            var vuefiles = vueUploadFiles.files;
                            var vueimages = vueUploadFiles.images;
                            var isExist = false;
                            for (var i = 0; i < vuefiles.length; i++) {
                                var obj = vuefiles[i];
                                if (obj.name == file.name) {
                                    delete files[index];
                                    isExist = true;
                                    break;
                                }
                            }
                            for (var i = 0; i < vueimages.length; i++) {
                                var obj = vueimages[i];
                                if (obj.name == file.name) {
                                    delete files[index];
                                    isExist = true;
                                    break;
                                }
                            }
                            if (!isExist) {
                                if (file.name.getFileType() == "image") {
                                    vueUploadFiles.images.push({ name: file.name, url: "#", isUpload: false });
                                } else {
                                    vueUploadFiles.files.push({ name: file.name, url: "#", isUpload: false });
                                }
                            }
                        });
                    },
                    done: function (res) { //上传完毕回调
                        if (res.isSuccess == 'true') {
                            var files = vueUploadFiles.files;
                            var images = vueUploadFiles.images;
                            files.forEach(function (obj) {
                                if (obj.name == res.name) {
                                    obj.url = res.url;
                                    obj.isUpload = true;
                                    return;
                                }
                            });
                            images.forEach(function (obj) {
                                if (obj.name == res.name) {
                                    obj.url = res.url;
                                    obj.isUpload = true;
                                    return;
                                }
                            });

                        }
                    },
                    error: function () { //请求异常回调
                    }
                });
            });
        });
        function deleteFileClick(name) {
            var files = vueUploadFiles.files;
            var images = vueUploadFiles.images;
            for (var i = 0; i < files.length; i++) {
                var obj = files[i];
                if (obj.name == name) {
                    files.remove(obj);
                    break;
                }
            }
            for (var i = 0; i < images.length; i++) {
                var obj = images[i];
                if (obj.name == name) {
                    images.remove(obj);
                    break;
                }
            }
        }
        //移除数组中的一个值
        Array.prototype.remove = function (val) {
            var index = this.indexOf(val);
            if (index > -1) {
                this.splice(index, 1);
            }
        };
        String.prototype.getFileType = function () {
            var suffix = this.split('.').pop();
            if (suffix == "jpg" || suffix == "png" || suffix == "gif" || suffix == "bmp" || suffix == "jpeg") {
                return "image";
            } else {
                return "file";
            }
        }
    </script>

</body>
</html>